package pkg.register;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import HibernateBeans.CustomerDetails;
import HibernateBeans.RepDetails;
import retail.bo.CustomerBo;

public class RegisterAction extends Action {
	private CustomerBo customerBo;
	public CustomerBo getCustomerBo() {
		return customerBo;
	}
	public void setCustomerBo(CustomerBo customerBo) {
		this.customerBo = customerBo;
	}
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();

		DynaActionForm myForm = (DynaActionForm) form;
		String userID=myForm.getString("userID");
		String role="";
		if(userID.startsWith("C")){
			CustomerDetails customer=customerBo.getUser(userID);
			role="Customer";
			if(customer.getUser()==null)
			session.setAttribute("user", customer);
			else {
				session.setAttribute("error", "User already has an username");
				return mapping.findForward("error");
			}
		}
		if(userID.startsWith("R")){
			RepDetails representative=customerBo.getRepDetails(userID);
			role="Representative";
			if(representative.getUser()!=null){
				session.setAttribute("error", "User already has an username");
				return mapping.findForward("error");
			}else
			session.setAttribute("user", representative);
		}
		session.setAttribute("role", role);
		return mapping.findForward("Registered");
	}
}